/**
 * 
 * 0001-0050:为服务器错误代码保留
 * */
package riki.org.com.net
{
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	
	import riki.org.com.handle.NetHandle;
	import riki.org.com.model.ModelLocator;

	public class Red5Connect extends NetConnection
	{
		public	static	var	rnc			:Red5Connect;
		public	static	var	Host		:String			=	"59.173.248.198";
		public	static	var	Port		:String			=	"1935";
		public	static	var	ServerApp	:String			=	"r5acs";
		
		private			var	nhandle		:NetHandle		=	new NetHandle();
		
		public function Red5Connect():void
		{
			if ( Red5Connect.rnc == null )
			{
				super();
			}
			else
			{
				throw new Error( "已经有一个正在保持的连接！" );
			}		
		}
		
		public static function GetNc():Red5Connect
		{
			if ( rnc == null )
			{
				rnc = new Red5Connect();
			}
			return rnc;
		}
				
		public function InitAdmin():void
		{			
			if(rnc.connected)
			{
				return;
			}
			
			var cStr:String	=	"rtmp://"+Host+":"+Port+"/"+ServerApp;
			
			if(Host==""||ServerApp=="")
			{
				cStr	=	"";
			}
			//连接监听
			rnc.addEventListener(NetStatusEvent.NET_STATUS        , onNetStatueEvent);
			rnc.addEventListener(IOErrorEvent.IO_ERROR            , onFindErrorEvent);			
			//连接服务器
			rnc.connect(cStr);
			//提升权限
		}
		
		/**
		 * 向服务器发送命令
		 * 
		 * */
		public function sendCommand(appName:String,functionName:String,refunctionName:String,...argc):void
		{
			if(!CommandCheck(appName,functionName,refunctionName,argc))
			{
				return;
			}
			rnc.call("sendCommand",null,appName,functionName,refunctionName,argc);
		}
		
		/**
		 * [Event Handle]
		 * 
		 * 命令参数出错的处理
		 * */
		private function CommandCheck(appName:String,functionName:String,refunctionName:String,...argc):Boolean
		{
			if(true)
			{
				trace("*********************************************************")
				trace("发送一条命令:")
				trace("服务器模块:"+appName)
				trace("函数名:"+functionName)
				trace("回调函数:"+refunctionName)
				trace("参数:"+argc)
				trace("*********************************************************")
			}
			//验证
			return true;
		}		
		
		/**
		 * [Event Handle]
		 * 
		 * NetConnection 连接出错（IO 错误）
		 * */
		public function onFindErrorEvent(e:*):void
		{
			nhandle.onFindErrorEvent(e);
		}
		
		/**
		 * [Event Handle]
		 * 
		 * 连接成功后的处理
		 * */
		private function ConnectSuccess(Message:String):void
		{
			nhandle.ConnectSuccess(Message);
			rnc.client	=	nhandle;
		}
				
		/**
		 * [Event Handle]
		 * 
		 * 连接关闭后的处理
		 * */
		private function ConnectClosed(Message:String):void
		{
			nhandle.ConnectClosed(Message);
		}
		
		
		/**
		 * [Event Handle]
		 * 
		 * 连接失败后的处理
		 * */
		private function ConnectFailed(CodeNumber:String,Message:String):void
		{
			nhandle.ConnectFailed(CodeNumber,Message);
		}
		
		/**
		 * [Event Handle]
		 * 
		 * NetConnection 对象返回的事件（含连接成功，服务器断开及连接异常）
		 * */
		private function onNetStatueEvent(e:NetStatusEvent):void
		{
			if(e.info.code == "NetConnection.Connect.Success")
			{				
				ConnectSuccess("成功连接。");
			}
			else if(e.info.code == "NetConnection.Connect.Closed")
			{
				ConnectClosed("成功关闭连接。");
			}		
			else if(e.info.code == "NetConnection.Connect.Failed")
			{
				ConnectFailed("0001","连接尝试失败。");
			}
			else if(e.info.code == "NetConnection.Connect.Rejected")
			{
				ConnectFailed("0002","连接尝试没有访问应用程序的权限。");
			}
			else if(e.info.code == "NetConnection.Connect.AppShutdown")
			{
				ConnectFailed("0003","正在关闭指定的应用程序。");
			}
			else if(e.info.code == "NetConnection.Connect.InvalidApp")
			{
				ConnectFailed("0004","连接时指定的应用程序名无效。");
			}
		}
	}
}